package com.supermap.localization;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.PropertyConfigurator;

import com.supermap.localization.api.LocalizationOperation;
import com.supermap.localization.database.LocalizationDaoImpl;
import com.supermap.localization.impl.DatabaseSampleComponent;
import com.supermap.localization.impl.FileExtracterImpl;
import com.supermap.localization.impl.FileReplacerImpl;
import com.supermap.localization.impl.LineExtracterImpl;
import com.supermap.localization.impl.LineReplacerImpl;
import com.supermap.localization.impl.LocalizationExport;
import com.supermap.localization.impl.LocalizationExtracter;
import com.supermap.localization.impl.LocalizationImport;
import com.supermap.localization.impl.LocalizationReplacer;
import com.supermap.localization.impl.PropertiesSamplesSaver;
import com.supermap.localization.impl.PropertiesSampleComponent;
import com.supermap.localization.impl.SampleSaveManager;
import com.supermap.localization.impl.SqliteSamplesSaver;
import com.supermap.localization.util.ConfigsUtil;
import com.supermap.localization.util.DBUtil;

public class Localization {

    private static LocalizationOperation operation;

    public static void main(String[] args) throws Exception {
        PropertyConfigurator.configure("log4j.properties");
        try {
            ConfigsUtil.initConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (args[0].equalsIgnoreCase("extract")) {
            LocalizationExtracter extracter = new LocalizationExtracter();
            FileExtracterImpl fileExtacter = new FileExtracterImpl();
            LineExtracterImpl lineExtracter = new LineExtracterImpl();
            fileExtacter.setLineExtracter(lineExtracter);
            extracter.setFileExtacter(fileExtacter);
            SampleSaveManager saveManager = new SampleSaveManager();
            PropertiesSamplesSaver propertiesSaver = new PropertiesSamplesSaver();
            PropertiesSampleComponent propSampleComp = new PropertiesSampleComponent();
            propertiesSaver.setPropSampleComponent(propSampleComp);
            saveManager.add(propertiesSaver);
            SqliteSamplesSaver sqliteSaver = new SqliteSamplesSaver();
            DatabaseSampleComponent dbSampleComp = new DatabaseSampleComponent();
            SqlSession sqlSession = DBUtil.getSqlSession();
            LocalizationDaoImpl localizationDao = new LocalizationDaoImpl(sqlSession);
            dbSampleComp.setLocalizationDao(localizationDao);
            sqliteSaver.setDbSampleComponent(dbSampleComp);
            saveManager.add(sqliteSaver);
            extracter.setSaveManager(saveManager);
            operation = extracter;
            operation.execute();
            sqlSession.commit();
            DBUtil.closeSqlSession();
        } else if (args[0].equalsIgnoreCase("update")) {
            LocalizationReplacer replacer = new LocalizationReplacer();
            FileReplacerImpl fileReplacer = new FileReplacerImpl();
            LineReplacerImpl lineReplacer = new LineReplacerImpl();
            fileReplacer.setLineReplacer(lineReplacer);
            replacer.setFileReplacer(fileReplacer);
            PropertiesSampleComponent propSampleComp = new PropertiesSampleComponent();
            replacer.setPropSampleComponent(propSampleComp);
            operation = replacer;
            operation.execute();
        } else if (args[0].equalsIgnoreCase("import")) {
            LocalizationImport imports = new LocalizationImport();
            DatabaseSampleComponent dbSampleComp = new DatabaseSampleComponent();
            SqlSession sqlSession = DBUtil.getSqlSession();
            LocalizationDaoImpl localizationDao = new LocalizationDaoImpl(sqlSession);
            dbSampleComp.setLocalizationDao(localizationDao);
            imports.setDbSampleComp(dbSampleComp);
            PropertiesSampleComponent propSampleComp = new PropertiesSampleComponent();
            imports.setPropSampleComp(propSampleComp);
            operation = imports;
            operation.execute();
            sqlSession.commit();
            DBUtil.closeSqlSession();
        } else if (args[0].equalsIgnoreCase("export")) {
            LocalizationExport export = new LocalizationExport();
            DatabaseSampleComponent dbSampleComp = new DatabaseSampleComponent();
            SqlSession sqlSession = DBUtil.getSqlSession();
            LocalizationDaoImpl localizationDao = new LocalizationDaoImpl(sqlSession);
            dbSampleComp.setLocalizationDao(localizationDao);
            export.setDbSampleComp(dbSampleComp);
            PropertiesSampleComponent propSampleComp = new PropertiesSampleComponent();
            export.setPropSampleComp(propSampleComp);
            operation = export;
            operation.execute();
            sqlSession.commit();
            DBUtil.closeSqlSession();
        }
    }

}
